package cn.zzuli.networkdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;

public class MainActivity extends AppCompatActivity {
    private String loginUrl = "https://www.wanandroid.com/user/login";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText et_name=findViewById(R.id.name);
        EditText et_pwd=findViewById(R.id.pwd);
        Button login=findViewById(R.id.button1);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = et_name.getText().toString();
                String pwd = et_pwd.getText().toString();
                new Thread() {
                    @Override
                    public void run() {
                        String login1 = login(name, pwd);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                JSONObject jsonobj=null;
                                try {
                                    jsonobj=new JSONObject(login1);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                int errorCode= jsonobj.optInt("errorCode");//错误码
                                String errorMsg= jsonobj.optString("errorMsg");//错误信息提示

                                if(errorCode==0){
                                    Intent intent=new Intent();
                                    intent.setAction("android.intent.action.VIEW");
                                    intent.setData(Uri.parse("https://www.wanandroid.com"));
                                    startActivity(intent);
                                    Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
                                }else{
                                    Toast.makeText(MainActivity.this,errorMsg,Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                }.start();
            }
        });
    }
    private String login(String name, String pwd) {
        try {
            URL url = new URL(loginUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setUseCaches(false);

            String data = "username=" + URLEncoder.encode(name, "utf-8") + "&password=" + URLEncoder.encode(pwd, "utf-8");
            OutputStream outputStream = conn.getOutputStream();
            outputStream.write(data.getBytes());
            outputStream.flush();
            if (conn.getResponseCode() == 200) {
                InputStream inputStream = conn.getInputStream();
                String s = stream2String(inputStream);
                System.out.println(s);
                return s;
            }
        } catch (IOException e) {
            e.printStackTrace();

        }
        return "";
}
    private String stream2String(InputStream is){
        BufferedReader br =new BufferedReader(new InputStreamReader(is));
        StringBuffer sb=new StringBuffer();
        String line;
        try{
            while((line=br.readLine())!=null) {
                sb.append(line);
            }
        }catch (Exception e){
        }
        return sb.toString();
    }
}